;;------------------------------------------------------------------------------
;;CScope
;;------------------------------------------------------------------------------
;;(add-to-list 'load-path "~/.emacs-config/plugins")
;;(require 'xcscope)

(add-hook 'c-mode-common-hook
		  '(lambda ()
			 (require 'xcscope)))
;;------------------------------------------------------------------------------
;;ac-clang
;;------------------------------------------------------------------------------
;; 加载clang  
(load-plugin "auto-complete-clang")
(require 'auto-complete-clang)

;; 添加c-mode和c++-mode的hook，开启auto-complete的clang扩展  
(defun wttr/ac-cc-mode-setup ()  
  (make-local-variable 'ac-auto-start)  
  (setq ac-auto-start nil)              ;;auto complete using clang is CPU sensitive  
  (setq ac-sources (append '(ac-source-clang ac-source-yasnippet) ac-sources))
  (setq ac-clang-flags 
		(list 
		 "/usr/include"
		 "/usr/include/c++/4.2.1"  
		 "/usr/include/c++/4.2.1"  
		 "/usr/include/c++/4.2.1/backward"  
		 "/usr/include/c++/4.2.1/ext"  
		 "/usr/include/c++/4.2.1/debug"  
		 "/usr/include/c++/4.2.1/tr1"  
		 "/usr/include/c++/4.2.1/bits"

		 "/usr/include/ffi"
		 "/usr/include/i386"
		 "/usr/include/python2.5"
		 "/usr/include/python2.6"
		 "/usr/include/python2.7"
		 "/usr/include/curl"
		 "/usr/include/cups"
		 "/usr/include/libxml2"
		 "/usr/include/memcached"
		 "/usr/include/readline"
		 "/usr/include/rpc"
		 "/usr/include/rpcsvr"
		 "/usr/include/openssl"
		 "/usr/include/dispatch"
		 

		 "/usr/local/include"
		 )))

(add-hook 'c-mode-hook 'wttr/ac-cc-mode-setup)  
(add-hook 'c++-mode-hook 'wttr/ac-cc-mode-setup) 

;;------------------------------------------------------------------------------
;;编译按键绑定
;;------------------------------------------------------------------------------
(defun cc-compile-debug ()
  "compile project"
  (interactive)
  (if (eq system-type 'windows-nt)
	  (progn(setq compile-cmd "cd build && cmake -DCMAKE_BUILD_TYPE=Debug  . -G \"MinGW Makefiles\" && make"))
	(progn(setq compile-cmd "cd build && cmake -DCMAKE_BUILD_TYPE=Debug .. && make "))) ;;(progn(setq compile-cmd "cmake -DCMAKE_BUILD_TYPE=Debug  ./ & make")))
  (make-directory "build" t)
  (compile-project compile-cmd))

(defun cc-compile-release ()
  "compile project"
  (interactive)
  (if (eq system-type 'windows-nt)
	  (progn(setq compile-cmd "cd build && cmake -DCMAKE_BUILD_TYPE=Release  ./ -G \"MinGW Makefiles\" && make"))
	(progn(setq compile-cmd "cd build && cmake -DCMAKE_BUILD_TYPE=Release  ./ && make")))
  (make-directory "build" t)
  (compile-project compile-cmd))

(defun cc-clean-project ()
  "clean roject"
  (interactive)
  (compile-project "echo clean!")
  ;;(delete-file "CMakeCache.txt")
  ;;(delete-file "cmake_install.cmake")
  (delete-directory "build" t))

(defun cc-fmt-buffer ()
  "Indent the whole buffer."
  (interactive)
  (save-excursion
	(indent-region (point-min) (point-max) nil)))

(defun cc-key-bindings ()
  (setq default-tab-width 4)
  (setq c-basic-offset 4)
  ;;(hs-minor-mode t)
  (setq tab-width 4 indent-tabs-mode nil)
  (c-set-style "K&R")

  (define-key c-mode-base-map [f7] 'cc-compile-debug)
  (define-key c-mode-base-map [C-f7] 'cc-compile-release)
  (define-key c-mode-base-map [S-f7] 'cc-clean-project)

  ;;C-c C-f 格式化代码
  (define-key c-mode-base-map (kbd "C-c C-f") 'cc-fmt-buffer))

(add-hook 'c-mode-common-hook 'cc-key-bindings )

(provide 'mod-cc)
